package question_factory;

import java.util.List;

import answers.IAnswer;
import answers.MultipleChoiceAnswer;
import answers.ShortAnswer;
import models.AbstractQuestion;
import models.ShortQuestion;

public class ShortQuestionFactory implements IQuestionFactory
{

	@Override
	public AbstractQuestion createQuestion(String encoding)
	{
		String [] tokens = encoding.split("\\s");
		if(tokens.length < 4)
		{
			return null;
		}
		
		try
		{
			double points = Double.parseDouble(tokens[2]);
			
			int answerPos = -1;
			for(int i = 3; i < tokens.length; i++)
			{
				String s = tokens[i];
				if(s.equals("-a") == true)
				{
					answerPos = i + 1;
					break;
				}
			}
			
			String questionText = new String();
			int textEndToken = 0;
			if(answerPos == -1)
			{
				textEndToken = tokens.length;
			}
			else
			{
				textEndToken = answerPos - 1;
			}
			
			for(int i = 3; i < textEndToken; ++i)
			{
				questionText += tokens[i] + " ";
			}
			String answerText = new String();
			if(answerPos > -1)
			{
				 for(int i = answerPos; i < tokens.length; ++i)
				 {
					 answerText += tokens[i] + " ";
				 }
			}
			
			ShortQuestion question = new ShortQuestion("short question", questionText, points);
			if(answerPos > -1)
			{
				ShortAnswer answer = new ShortAnswer(answerText);
				question.setItsIAnswer(answer);
			}
			
			return question;
			
		}
		catch(NumberFormatException e)
		{
			System.out.println("Wrong question format");
			return null;
		}
	}

}
